package cn.tedu.dao.impl;

import cn.tedu.dao.IUser;
import cn.tedu.entity.User;
import cn.tedu.exception.ErrorException;
import cn.tedu.util.C3p0DBUtil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *
 * </pre>
 *
 * @author restyhap
 * @since 2019/10/28 15:31
 */
public class IUserImpl implements IUser {
    @Override
    public int add(User user) {
        String sql = "insert tb_user (name,password) values (?,?);";
        Object objs []= new String[]{user.getName(),user.getPassword()};
        int i = update(sql,objs);
        return i;
    }
    @Override
    public int delete(String id) {
        int idvalue = Integer.valueOf(id);//将字符串转换成int值
        String sql = "delete from tb_user where id=?";
        return update(sql,new Object[]{idvalue});
    }
    @Override
    public int change(User user) {
        String sql = "update tb_user set name = ?,password=? where id= ? ; ";
        return update(sql,new Object[]{user.getName(),user.getPassword(),user.getId()});
    }
    public int change1(User user){
        int i = -1;
        String sql = "update tb_user set name = ?,password=? where id= ? ; ";
        Connection conn = C3p0DBUtil.getConnection();
        PreparedStatement prestat =  null;
        try {
           prestat  =  conn.prepareStatement(sql);
           prestat.setInt(1,user.getId());
           prestat.setString(2,user.getName());
           prestat.setString(3,user.getPassword());
            i = prestat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    @Override
    public List<User> queryAll() {
        List<User> list = new ArrayList();
        String sql = "select id,name,password from tb_user";
        Connection conn = C3p0DBUtil.getConnection();
        PreparedStatement prestat = null;
        ResultSet rs = null;
        try {
            prestat = conn.prepareStatement(sql);
            rs =  prestat.executeQuery();
            while(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                throw new ErrorException("产生了sql异常");
            } catch (ErrorException e1) {
                e1.printStackTrace();
            }
        }finally {
            C3p0DBUtil.close(conn,prestat,rs);
        }
        return list;
    }
    @Override
    public User queryUser(String name) {
        User user =null;
        String sql = "select id,name,password from tb_user where name=?";
        Connection conn = C3p0DBUtil.getConnection();
        PreparedStatement prestat = null;
        ResultSet rs = null;
        try {
            prestat = conn.prepareStatement(sql);
            prestat.setString(1,name);
            rs =  prestat.executeQuery();
            if(rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            C3p0DBUtil.close(conn,prestat,rs);
        }
        return user;
    }
    /**根据之前分析,增删改的方法都是一样的.所以创建出对应的update方法.
     *  当前方法是了解方法.
     * */
    public int update(String sql,Object... objs){
        int i = -1;
        Connection conn = C3p0DBUtil.getConnection();
        PreparedStatement prestat = null;
        try {
            prestat = conn.prepareStatement(sql);
            for (int j = 0; j < objs.length; j++) {
                prestat.setObject(j+1,objs[j]);
            }
            i = prestat.executeUpdate();//执行sql语句.
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            C3p0DBUtil.close(conn,prestat);
        }
        return i ;
    }
}
